<?php

namespace App\Service\Index;

use App\Constants\ErrorCode;
use App\Constants\InviteVideoCode;
use App\Helpers\ConfigUtil;
use App\Model\CallBase;
use App\Model\DiscountVideoUsers;
use App\Model\User;
use App\Model\UserFreeCall;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Model\VideoCallAuditConfig;
use App\Service\CallBaseService;

/**
 * 拨打视频电话
 */
class VideoCallService extends CallBaseService
{
    public function handle()
    {
        //自己的ID
        $userId = $this->getUserId();
        //呼叫来源
        $callFrom = $this->request->input('from', 0);
        //主播用户ID
        $hostUserId = $this->request->input('host_user_id', 0);
        //不能打给自己
        if ($userId == $hostUserId) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '不能拨打给自己~');
        }

        //被叫方
        if (User::checkOfficial($hostUserId)) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '暂无法拨打给小助手~');
        }

        //天天福利-随机获取主播
        if (CallBase::FROM_DAILY_FREE == $callFrom && !$hostUserId) {
            return $this->dailyFreeCall($userId, VideoCall::class);
        }

        //主播呼叫
        $selfProfile = UserProfile::getProfile($userId);
//        if (UserProfile::checkRoleHost($selfProfile->role)) {
//            return $this->fromHostCall($userId, $hostUserId, VideoCall::class, $selfProfile);
//        }
        if ($selfProfile->gender == 0) {
            if ($selfProfile->avatar_vrfy == 1) {
                return $this->fromHostCall($userId, $hostUserId, VideoCall::class, $selfProfile);
            } else {
                return $this->setError(ErrorCode::REAL_AVATAR_NOT_VRFY, '还未通过真人头像认证～');
            }
        }

//        $callFrom = CallBase::FROM_MAIN;
        $callFrom = $callFrom == CallBase::FROM_DISCOUNT_CALL ? CallBase::FROM_DISCOUNT_CALL : CallBase::FROM_MAIN;
        //小助手呼叫
        if (User::checkOfficial($userId)) {
            $coinSetting = 0;
            $hostProfile = UserProfile::getProfile($hostUserId);
        } else {
            $freeCall = 0;
            //免费券
            $version = $this->request->input('version', '1.0.0');
            $useFree = $this->request->input('use_free', 0);
            if ($version >= '1.0.0' && $useFree) {
                $freeCall = UserFreeCall::checkValid(
                    $userId,
                    date('Y-m-d'),
                    UserFreeCall::STATUS_OK,
                    'count'
                );
                $callFrom = $freeCall ? CallBase::FROM_DAILY_FREE : CallBase::FROM_MAIN;
            }


            if (VideoCall::getInviteVideoTotal($userId) < InviteVideoCode::TOTAL_MAX_ANSWER_NUM) {
                $callFrom = VideoCall::FROM_INVITE_CALL;
//                $freeCall = 1;
            }

            //检测拨打权限
            $checkCoin = $freeCall > 0 ? false : true;
            $checkResult = $this->checkCall($userId, $hostUserId, VideoCall::CALL_TYPE, $checkCoin, $callFrom);
            if ($checkResult['ret'] != 0) return $checkResult;

            //免费福利
            if ($freeCall) {
                    $coinSetting = ConfigUtil::getDailyFreeCoin(VideoCall::CALL_TYPE);
            } else {
                $coinSetting = $checkResult['hostCoinSetting'];
            }
            $hostProfile = $checkResult['hostProfile'];
        }

        if ($callFrom == CallBase::FROM_DISCOUNT_CALL) {
            $hostProfile['video_coin_setting'] = DiscountVideoUsers::DISCOUNT_COIN;
        }

        $callMark = 1;
        if ($callFrom == VideoCall::FROM_INVITE_CALL)
        {
            $callMark =3;
            list($userId,$hostUserId) = array($hostUserId,$userId);
//            $coinSetting = 0;
        }

        //修改以前无效状态
        VideoCall::finishOldCall($userId);

//        $callMark = 1;
//        if (VideoCall::getInviteVideoTotal($userId) < InviteVideoCode::TOTAL_MAX_ANSWER_NUM) {
//            $callFrom = VideoCall::FROM_INVITE_CALL;
//            $coinSetting = 0;
//            $callMark =3;
//            list($userId,$hostUserId) = array($hostUserId,$userId);
//        }

        //通话记录
        $ret = VideoCall::addRecord($userId, $hostUserId, $callFrom, $coinSetting,$callMark);
        //呼叫回调
        $this->callHook($hostUserId, $userId, VideoCall::CALL_TYPE);
        // 鉴黄配置
        $config = (new VideoCallAuditConfig())->getConfig($userId);
        $ret = [
            'permit' => 1,
            'user_info' => UserProfile::buildHostItem($hostProfile),
            'support_face' => User::checkFaceSupport($hostUserId),
            'jHConfig' => $config
        ];
        log_user_result($userId, "ChatUserId:{$hostUserId}==" . json_encode($ret));
        return $ret;
    }
}
